package com.dzf.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 用户对象
 */
@Document("t_user")
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class User implements UserDetails {
    /**
     * id：用户id
     * username：用户名
     * password：密码
     * name：网名
     */
    @Id
    private String id;
    private String username;
    private String password;
    private String name;
    private String role;


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities()
    {
        List<GrantedAuthority> list = new ArrayList<>();
        if (this.role==null) {
            return null;
        }
        String[] split = this.role.split(",");
        for (int i = 0; i < split.length; i++) {
            String role = "ROLE_";
            role = role + split[i];
            SimpleGrantedAuthority simple = new SimpleGrantedAuthority(role);
            list.add(simple);
        }
        System.out.println("list++++++++++++++++++="+list);
        return list;

    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {//账号激活
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {//账号是否被锁
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {//
        return true;
    }

    @Override
    public boolean isEnabled() {//账号是否激活
        return true;
    }

    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", role='" + role + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}
